import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'

Vue.use(Router)


const router=new Router({
  routes: [
    {path: '/',redirect: '/login'},
    {path: '/login',component: Login },
    {path: '/home',component: Home}
  ]
})
//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to 即将要访问的路径
  //from代表从那个路径跳转而来
  //  next()放行  next('/login') 强制跳转
  if(to.path==='/login')return next()
  //获取tokenBearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2MzEwOTU4MTYsImV4cCI6MTYzMTE4MjIxNn0.jNed7TxyBsD_oZYuYADVj0XAvh_C0kOmImps9KQztf4
  const tokenStr=window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})
export default router